<?php
/**
 * Created by PhpStorm.
 * User: zy-l
 * Date: 2019/6/19
 * Time: 1:23 AM
 */
namespace App\HttpController\Admin;

use App\HttpController\AdminBase;
use EasySwoole\Validate\Validate;

/**
 * 操作行为管理
 * Class Adv
 * @package App\HttpController\Admin
 */
class Action extends AdminBase
{
    /**
     * 行为模型
     * @return \App\Model\Action
     */
    public function getActionModel(){
        return new \App\Model\Action();
    }

    /**
     * 日志模型
     * @return \App\Model\ActionLog
     */
    public function getActionLogModel(){
        return new \App\Model\ActionLog();
    }

    /**
     * 列表
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function index(){
        if ($this->isPost()) {
            $where=array();
            $title=$this->postParam("title");
            if(!empty($title)){
                $where['title|like']='%'.$title.'%';
            }
            $name=$this->postParam("name");
            if(!empty($name)){
                $where['name|like']='%'.$name.'%';
            }
            return $this->writePageJson('列表数据',$this->getActionModel()->getPageAll($where,$this->postParam('page',0)));
        }
        return $this->fetch();
    }

    /**
     * 添加
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function add(){
        if($this->isPost()){
            return $this->writeJson('操作成功',$this->getActionModel()->updateOrCreateData($this->postParams(),["id"=>$this->postParam("id")]));
        }
        return $this->fetch('add');
    }

    /**
     * 编辑
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function edit(){
        if($this->isPost()){
            return $this->writeJson('操作成功',$this->getActionModel()->updateOrCreateData($this->postParams(),["id"=>$this->postParam("id")]));
        }
        $info=$this->getActionModel()->findOne(['id'=>$this->getParam('id')]);
        $this->assign(['info'=>$info]);
        return $this->fetch('edit');
    }

    /**
     * 数据删除
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function del(){
        $id=$this->postParam('id');
        if($this->getActionModel()->delData(['id'=>$id])){
            return $this->writeJson('删除成功',$this->getActionLogModel()->delData(['action_id'=>$id]));
        }
    }

    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        return $validate;
    }
}